package cn.zt.service.impl;

import cn.zt.common.Result;
import cn.zt.domain.Bankcard;
import cn.zt.domain.TProductDetails;
import cn.zt.feign.BankcardFeign;
import cn.zt.feign.FeignBankCard;
import cn.zt.mapper.TProductDetailsMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zt.domain.TUserFinancial;
import cn.zt.service.TUserFinancialService;
import cn.zt.mapper.TUserFinancialMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 21889
* @description 针对表【t_user_financial(理财产品用户中间表)】的数据库操作Service实现
* @createDate 2024-09-03 19:18:05
*/
@Service
public class TUserFinancialServiceImpl extends ServiceImpl<TUserFinancialMapper, TUserFinancial>
    implements TUserFinancialService{

    @Resource
    private TUserFinancialMapper tUserFinancialMapper;

    @Resource
    private FeignBankCard bankcardFeign;

    @Resource
    private TProductDetailsMapper productDetailsMapper;

    @Override
    public Result buying(TUserFinancial userFinancial) {
        System.out.println(userFinancial);
        //调用openfeign查询出来更具银行卡查出来的数据
        if (userFinancial.getBankCard() != null){
            Bankcard bankCard = bankcardFeign.getBankCard(userFinancial.getBankCard());
            if (bankCard != null){
                //判断支付密码是否正确
                if (bankCard.getPayPassword().equals(userFinancial.getPossWord())){
                    //减余额
                    int i = bankcardFeign.jiBalance(userFinancial.getBankCard(), userFinancial.getMoney());
                    if (i > 0){
                        //减份数
                        TProductDetails tProductDetails = productDetailsMapper.selectOne(Wrappers.<TProductDetails>lambdaQuery().eq(TProductDetails::getId, userFinancial.getFinancialId()));
                        if (tProductDetails != null){
                            tProductDetails.setMaxNumber(tProductDetails.getMaxNumber()-userFinancial.getNumber());
                        }else {
                            return Result.error("请输入正确的份数");
                        }
                        int i1 = productDetailsMapper.updateById(tProductDetails);
                        if (i1 > 0){
                            //添加中间表
                            int insert = tUserFinancialMapper.insert(userFinancial);
                            return insert>0 ?Result.success():Result.error("购买失败");
                        }
                    }else {
                        return Result.error("请输入正确的份数");
                    }
                }else {
                    return Result.error("请输入正确的密码");
                }
            }else {
                return Result.error("请输入正确的银行卡");
            }
        }
        return Result.error("请输入正确的银行卡");
    }

    @Override
    public Result getByUserIdList(Integer userId) {
        List<TUserFinancial> tUserFinancials = tUserFinancialMapper.selectList(Wrappers.<TUserFinancial>lambdaQuery().eq(TUserFinancial::getUserId, userId));
        for (TUserFinancial tUserFinancial : tUserFinancials){
            if (tUserFinancial.getFinancialId() != null){
                TProductDetails tProductDetails = productDetailsMapper.selectOne(Wrappers.<TProductDetails>lambdaQuery().eq(TProductDetails::getId, tUserFinancial.getFinancialId()));
                if (tProductDetails != null){
                    tUserFinancial.setProductDetails(tProductDetails);
                }
            }
        }
        return Result.success(tUserFinancials);
    }

    @Override
    public Result getRansom(TUserFinancial userFinancial) {
        if (userFinancial.getBankCard() != null){
            //查询该卡号的支付密码
            Bankcard bankCard = bankcardFeign.getBankCard(userFinancial.getBankCard());
            if (bankCard != null){
                //判断支付密码是否正确
                if (bankCard.getPayPassword().equals(userFinancial.getPossWord())){
                    //根据id查询当前这个数据
                    TUserFinancial tUserFinancial = tUserFinancialMapper.selectOne(Wrappers.<TUserFinancial>lambdaQuery().eq(TUserFinancial::getId, userFinancial.getId()));
                    if (tUserFinancial != null){
                        //把中间表的份数进行减少
                        tUserFinancial.setNumber(tUserFinancial.getNumber() - userFinancial.getNumber());
                        int i = tUserFinancialMapper.updateById(tUserFinancial);
                        if (i > 0){
                            if (tUserFinancial.getNumber()<1){
                                tUserFinancialMapper.deleteById(tUserFinancial.getId());
                            }
                            //把赎回的份数加到余额里面
                            int i1 = bankcardFeign.jaBalance(userFinancial.getBankCard(), Math.toIntExact(userFinancial.getNumber()));
                            if (i1 > 0){
                                return Result.success();
                            }else {
                                return Result.error("赎回失败");
                            }
                        }else {
                            return Result.error("赎回失败，无法执行");
                        }
                    }else {
                        return Result.error("赎回失败，请重试");
                    }
                }else {
                    return Result.error("请输入正确支付密码");
                }
            }else {
                return Result.error("请输入正确的银行卡");
            }
        }
        List<TUserFinancial> tUserFinancials = tUserFinancialMapper.selectList(Wrappers.<TUserFinancial>lambdaQuery());
        for (TUserFinancial tUserFinancial : tUserFinancials){
            if (tUserFinancial.getNumber() < 1 ){
                tUserFinancialMapper.deleteById(tUserFinancial);
            }
        }
        return Result.error("请输入正确的银行卡");
    }
}




